package com.jvtd.eyl.utils;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;

import com.jvtd.eyl.R;
import com.jvtd.utils.MagicIndicatorUtils;
import com.jvtd.utils.UiUtils;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView;

import java.util.List;

public class AppIndicatorUtils extends MagicIndicatorUtils {
    public static void initSurroundedMagicIndicator(Context context, ViewPager viewPager, MagicIndicator magicIndicator, List<String> list, OnMagicIndicatorChangeListener onMagicIndicatorChangeListener){
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setLeftPadding(UIUtil.dip2px(context,20));
        commonNavigator.setRightPadding(UIUtil.dip2px(context,20));
        commonNavigator.setAdapter(new CommonNavigatorAdapter()
        {
            @Override
            public int getCount()
            {
                return list == null ? 0 : list.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index)
            {
                ClipPagerTitleView clipPagerTitleView = new ClipPagerTitleView(context);
                clipPagerTitleView.setText(list.get(index));
                clipPagerTitleView.setTextSize(UiUtils.sp2px(context, 16));
                clipPagerTitleView.setTextColor(ContextCompat.getColor(context, R.color.color_999));
                clipPagerTitleView.setClipColor(ContextCompat.getColor(context, R.color.color_fff));
                clipPagerTitleView.setOnClickListener(v -> {
                    if (onMagicIndicatorChangeListener != null)
                        onMagicIndicatorChangeListener.onChange(index);
                });
                return clipPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context)
            {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                float navigatorHeight = context.getResources().getDimension(R.dimen.dp_36);
                float yOffSet = UiUtils.dp2px(context, 1);
                float lineHeight = navigatorHeight - 2 * yOffSet;
                indicator.setLineHeight(lineHeight);
                indicator.setRoundRadius(UiUtils.dp2px(context, 3));
                indicator.setYOffset(yOffSet);
                indicator.setColors(ContextCompat.getColor(context, R.color.color_937f69));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        if (viewPager != null)
            ViewPagerHelper.bind(magicIndicator, viewPager);
    }
}
